// pages/MonthlyPay/MonthlyPay.js
var util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    ulSelectText: '貸款金額:',
    ulmenushow: true,
    ulSelectshow: false,
    unittext: '萬',
    Price: '',//物業價格
    loanType: '1',
    loan: '',//貸款
    rate: '',//年利率
    timelimit: '',//還款期,
    _erate: true,
    _eyear: true,
    _percen: true,
    _price: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.removeStorageSync('kPrice'),//物業價格
      wx.removeStorageSync('kloan'),//貸款
      wx.removeStorageSync('kloanType'),
      wx.removeStorageSync('krate'),//年利率
      wx.removeStorageSync('ktimelimit')//還款期
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  PriceInput: function (e) {
    this.setData({
      Price: e.detail.value
    })
  },
  loanInput: function (e) {
    var lovale = parseFloat(e.detail.value)
    if (isNaN(lovale))
      lovale = 0;


    var pricevalue = parseFloat(this.data.Price)

    if (isNaN(pricevalue))
      pricevalue = 0;


    if (this.data.loanType == '1') {
      console.log(lovale)
      console.log(parseFloat(this.data.Price))
      if (lovale > parseFloat(this.data.Price)) {
        this.setData({
          _price: false
        })
        return ''
      }
    }
    
    if (this.data.loanType == '2') {
      if (lovale > 100) {
        this.setData({
          _percen: false
        })
        return ''
      }

    }


    this.setData({
      loan: e.detail.value,
      _percen: true,
      _price: true
    })
  },
  rateInput: function (e) {
    var rate = parseFloat(e.detail.value)
    if (rate < 0 || rate >= 100) {
      this.setData({ _erate: false })
      return ''
    }
    var srate = rate.toString().split(".")[1];
    if (srate != undefined) {
      if (srate.length > 3) {
        return util.Numberdigits(rate, 3)
      }
    }
    this.setData({
      rate: e.detail.value,
      _erate: true
    })
  },
  ulSelectTap: function (e) {

    this.setData({
      ulSelectshow: true,
      ulmenushow: false

    });

  },
  selectGenreTap: function (e) {
    var stype = e.currentTarget.dataset.key;
     
    if (stype == '1') {


      this.setData({
        loanType: stype,
        ulSelectshow: false,
        ulmenushow: true,
        ulSelectText: '貸款金額',
        unittext: '萬',
        _percen: true,
        _price: true
      });
    }
    else {
      this.setData({
        loanType: stype,
        ulSelectshow: false,
        ulmenushow: true,
        ulSelectText: '貸款比例',
        unittext: '%',
        _percen: true,
        _price: true
      });
    }
  },

  RepaymentTimeInput: function (e) {
    var RepaymentTime = parseFloat(e.detail.value)
    if (RepaymentTime < 1 || RepaymentTime > 50) {
      this.setData({
        _eyear: false
      })
      return ''
    }
    this.setData({
      timelimit: e.detail.value,
      _eyear: true
    })
    /*
    console.log(e.detail.value.length)
    
  
     var RepaymentTime = parseFloat(e.detail.value)
    if (!isNaN(RepaymentTime)) {
      if (RepaymentTime>40)
      {
        if (e.detail.value.length>=2)
         {  var s = e.detail.value.substring(0, e.detail.value.length - 1)
         
           
           
         }
       

        wx.showModal({
          showCancel:false,
          title: '提示',
          content: '還款年限請1~40之間的年限',

        })
      }
    }*/

  },
  calculateTap: function (e) {

    if (this.data.Price.length <= 0 || this.data.loan.length <= 0 || this.data.rate.length <= 0 || this.data.timelimit.length <= 0) {
      wx.showToast({
        title: '請填寫完整的供款信息',
        icon: 'loading'
      })
      return false;
    }
    if (this.data.loanType == '1') {

      if (parseFloat(this.data.loan) > parseFloat(this.data.Price)) {
        this.setData({
          _price: false
        })
        return ''
      }
    }
    if (this.data.loanType == '2') {
      var _loan = parseFloat(this.data.loan);
      if (_loan > 100) {
        this.setData({
          _percen: false
        })
        return false;
      }
    }
    var rate = parseFloat(this.data.rate)
   
    if (rate <= 0.001 || rate > 100) {
      this.setData({ _erate: false })
      return false;
    }
    wx.setStorageSync('kPrice', this.data.Price),//物業價格
      wx.setStorageSync('kloan', this.data.loan),//貸款
      wx.setStorageSync('kloanType', this.data.loanType),
      wx.setStorageSync('krate', this.data.rate),//年利率
      wx.setStorageSync('ktimelimit', this.data.timelimit)//還款期
    wx.navigateTo({
      url: '/pages/MonthlyPay/ComputingResult/ComputingResult',
    })

  }
})